package cn.tedu.basic;

import java.io.*;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Scanner;

//测试File文件流
public class Test1_File {
    public static void main(String[] args) throws IOException {
        File f = new File("D:\\a\\b\\2.txt");
        System.out.println(f.isFile());
        f.getCanonicalPath();
        f.isDirectory();
        f.createNewFile();
        f.canRead();
        f.canWrite();
        f.exists();
        f.getAbsoluteFile();
        f.getName();
        f.getParent();
        f.getParentFile();
        f.getPath();
        f.length();
        String[] s = f.list();
        System.out.println(Arrays.toString(s));
        File[] s1 = f.listFiles();
        System.out.println(Arrays.toString(s1));
        System.out.println(s[2].length());
        String s2 = s[0];
        System.out.println(s2);
        File s3 = s1[2];
        System.out.println(s3);
        f.getAbsolutePath();
        f.mkdir();
        f.mkdirs();
        //获取指定文件夹里所有资源名称
        File f1 = new File("D:\\a\\b");
        File[] f2 = f1.listFiles();
        for (int i = 0; i < f2.length; i++) {
            File f3 = f2[i];
            if (f3.isFile()) {
                System.out.println(f3.getName());
            } else if (f3.isDirectory()) {
                System.out.println(f3.getName());
            }
        }
        Method();
        method1();
        method3();
        method4();

    }

    public static void Method() {
        //获取文件夹里文件的总大小
        File f4 = new File("D:\\you");
        File[] f5 = f4.listFiles();
        long l = 0;
        for (int i = 0; i < f5.length; i++) {
            File f6 = f5[i];
            if (f6.isFile()) {
                l = f6.length() + l;
            } else if (f6.isDirectory()) {
                Method();
            }
        }
        System.out.println(l);
    }

    //文件夹里所有文件夹目录里的文件总大小
    public static long Method2(File wj) {
        System.out.println("请输入一个文件路径：");
        String wjj = new Scanner(System.in).nextLine();
        File dir = new File(wjj);
        long to = Method2(dir);
        File[] f9 = dir.listFiles();
        long l1 = 0;
        for (int i = 0; i < f9.length; i++) {
            File f10 = f9[i];
            if (f10.isFile()) {
                l1 = f10.length() + l1;
            } else if (f10.isDirectory()) {
                l1 = l1 + Method2(f10);
            }
        }
        System.out.println(l1);
        return to;
    }
    //测试读取流
    public static void method1() throws IOException {
        File fi =new File("D:\\");
        InputStream in =new FileInputStream(fi);
        InputStream In =new FileInputStream("D:\\");
        int i1=In.read();
        System.out.println(i1);
        int i2 =In.read();
        System.out.println(i2);
        In.close();
        int  i3 =0;
        while ((i3=In.read())!=-1){
            System.out.println(i3);
        }
        In.close();
        InputStream Inp =new BufferedInputStream(new FileInputStream("D;"));
        int i4 =0;
        while ((i4=Inp.read())!=-1){
            System.out.println(i4);
        }
        Inp.close();
    }
    //测试写出
    public static void method3() throws IOException {
        File fe =new File("D:");
        OutputStream fe1 =new FileOutputStream(fe,true);
        OutputStream fe2=new BufferedOutputStream(new FileOutputStream("D\\",true));
        fe1.write(98);
        fe1.write(99);
        OutputStream fe3 =new FileOutputStream("D:\\");
        fe3.write(97);
    }
    //删除文件夹里所有的文件夹目录下的文件
    public static void method4(){
        System.out.println("请输入文件夹路径:");
        String ff =new Scanner(System.in).nextLine();
        File ff1 =new File(ff);
        File[] ff2=ff1.listFiles();
        for (int i = 0; i <ff2.length ; i++) {
            File ff3=ff2[i];
            if(ff3.isFile()){
                ff3.delete();
            }else if(ff3.isDirectory()){
                method4();
            }
        }

    }


}
